#!/bin/bash

# 此脚本需要在nginx节点执行,包括检测nginx进程状态、端口状态、等待中连接数、读取连接数、写入连接数

# 系统ID
system_id=$(cat config.cnf | grep SystemId | awk -F "=" '{print $2}')
# 告警存放文件夹
alert_dir=$(cat config.cnf | grep AlertDir | awk -F "=" '{print $2}')
# 告警缓存文件
alarm_file_tmp="nginx_check_alarm.txt"
echo -n "" >${alarm_file_tmp}
# 本机ip
local_ip=$(ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addr:")
# nginx端口
nginx_port=$(cat config.cnf | grep NginxPort | awk -F "=" '{print $2}')
nginx_protocol=$(cat config.cnf | grep NginxProtocol | awk -F "=" '{print $2}')
nginx_waiting=$(cat config.cnf | grep NginxWaiting | awk -F "=" '{print $2}')
nginx_reading=$(cat config.cnf | grep NginxReading | awk -F "=" '{print $2}')
nginx_writing=$(cat config.cnf | grep NginxWriting | awk -F "=" '{print $2}')

execute_cycle=$(cat config.cnf | grep ExecuteCycle | awk -F "=" '{print $2}')

# nginx进程状态监控
nginx_pro_check() {
    nginx_pro=$(ps -ef | grep nginx | grep -v grep | grep -v cmpnginx | grep -v nginx_check | wc -l)
	if [[ ${nginx_pro} == 0 ]]; then
        # 进程状态不存在,告警
         ./gen_alarm.sh -g "应用进程监控" "nginx服务状态异常,nginx进程不存在,IP:$(hostname -I)" 3 "nginx进程" "nginx_pro" ${alarm_file_tmp}
    else
        # 告警存在,恢复原告警
        ./gen_alarm.sh -r "应用进程监控" "nginx进程恢复正常,IP:$(hostname -I)" "nginx进程" "nginx_pro" ${alarm_file_tmp}

        # 端口检查
        nginx_port_check
        # 连接数检查
        nginx_conn_num_check

    fi
}

# nginx运行端口监控
nginx_port_check() {
	for port in $(echo ${nginx_port} | sed "s/,/ /g"); do
		port_info=$(netstat -nplt | grep ":${port} ")
		if [[ ${port_info} == '' ]]; then
			# 端口未监听,告警
			./gen_alarm.sh -g "端口状态" "nginx服务端口[${port}]未监听,IP:$(hostname -I)" 3 "nginx端口状态" "nginx_port" ${alarm_file_tmp}
		else
			# 端口正常且存在告警,需要恢复告警
			./gen_alarm.sh -r "端口状态" "nginx服务端口[${port}]恢复正常,IP:$(hostname -I)" "nginx端口状态" "nginx_port" ${alarm_file_tmp}
		fi
	done
}

# 连接、读取、写入连接数
nginx_conn_num_check() {
    conn_info=$(curl ${nginx_protocol}://localhost:80/status |grep -i waiting)
    if [[ ${conn_info} != '' ]]; then
        waiting=$(echo ${conn_info} | awk '{print $6}')
        if [[ ${waiting} -gt ${nginx_waiting} ]]; then
             ./gen_alarm.sh -g "连接状态" "nginx等待连接数超出阈值${nginx_waiting},当前值${waiting},IP:$(hostname -I)" 2 "nginx等待连接数" "nginx_conn_waiting" ${alarm_file_tmp}
        else
            ./gen_alarm.sh -r "连接状态" "nginx等待连接数恢复正常,阈值${nginx_waiting},当前值${waiting},IP:$(hostname -I)" "nginx等待连接数" "nginx_conn_waiting" ${alarm_file_tmp}
        fi

        writing=$(echo ${conn_info} | awk '{print $4}')
        if [[ ${writing} -gt ${nginx_writing} ]]; then
             ./gen_alarm.sh -g "连接状态" "nginx写入连接数超出阈值${nginx_writing},当前值${writing},IP:$(hostname -I)" 2 "nginx写入连接数" "nginx_conn_writing" ${alarm_file_tmp}
        else
            ./gen_alarm.sh -r "连接状态" "nginx写入连接数恢复正常,阈值${nginx_writing},当前值${writing},IP:$(hostname -I)" "nginx写入连接数" "nginx_conn_writing" ${alarm_file_tmp}
        fi

        reading=$(echo ${conn_info} | awk '{print $2}')
        if [[ ${reading} -gt ${nginx_reading} ]]; then
             ./gen_alarm.sh -g "连接状态" "nginx读取连接数超出阈值${nginx_reading},当前值${reading},IP:$(hostname -I)" 2 "nginx读取连接数" "nginx_conn_reading" ${alarm_file_tmp}
        else
            ./gen_alarm.sh -r "连接状态" "nginx读取连接数恢复正常,阈值${nginx_reading},当前值${reading},IP:$(hostname -I)" "nginx读取连接数" "nginx_conn_reading" ${alarm_file_tmp}
        fi
    fi
}

while true; do
    holidays_date=$(cat config.cnf | grep Holidays | awk -F "=" '{print $2}' | sed "s/,/ /g")
	current_date=$(date +'%Y-%m-%d')
	echo $holidays_date | grep -o $current_date >> /dev/null
	if [[ $? -eq 0 ]]; then
		exit 0
	else
		echo -n "" >${alarm_file_tmp}
		nginx_pro_check
		# 将缓存告警放入告警文件夹
		./gen_alarm.sh -s ${alarm_file_tmp}
		sleep ${execute_cycle}
	fi
done
